/*******************************************************************************
 * * Copyright 2011 Impetus Infotech.
 *  *
 *  * Licensed under the Apache License, Version 2.0 (the "License");
 *  * you may not use this file except in compliance with the License.
 *  * You may obtain a copy of the License at
 *  *
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  * See the License for the specific language governing permissions and
 *  * limitations under the License.
 ******************************************************************************/
package com.scalyze.apps.socialanyltics.datastore.persistence;

import java.io.IOException;
import java.util.Iterator;

import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * The Class HBaseWriter.
 * 
 * @author impetus
 */
public class HBaseWriter implements Writer {
	/** the log used by this class. */
	// private static Log log = LogFactory.getLog(HBaseWriter.class);

	@SuppressWarnings("unchecked")
	@Override
	public void writeColumns(HTable htable, JSONObject column)
			throws IOException {

		try {
			Iterator<String> iter = column.keys();
			Put p = new Put(Bytes.toBytes(column.getString("id_str")));

			while (iter.hasNext()) {
				String qualifier = iter.next();
				if (!qualifier.equals("id_str")) {
					String value = column.getString(qualifier);
					p.add(Bytes.toBytes(qualifier + ":" + qualifier), System
							.currentTimeMillis(), Bytes.toBytes(value));
				}
			}
			htable.put(p);
		} catch (JSONException e) {
			throw new RuntimeException(e.getMessage());
		}
	}

}
